<?php
/**
 * CreateTime: 2022/5/7 11:18
 */

namespace App\common;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\AppFunc;
use App\Func\PathFunc;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Models\File\FileModel;
use App\Models\System\ConfigModel;
use App\Tools\DingTalk\DingTalkUploadFacade;
use Illuminate\Support\Facades\Storage;

class cFile
{
    public function getUploadFilesIds($files){

		$ar = array_column($files,"file_id");
	
        return  implode(",",$ar);
    }

    public function getFileIds($fileIds){
        $formatFileIds = explode(",",$fileIds);
        return (new FileModel())->getFilesByIds($formatFileIds) ?? [];
    }

    public function getFileId($fileId){
        return (new FileModel())->getFileById($fileId) ?? [];
    }

    public function setFileToDingTalk($fileId,$parentSpaceName,$createSpaceName){
        # 获取当前登录用户的unionId
        # 获取配置表中的unionId
        $configModel =  new ConfigModel();
        $unionId = $configModel->getConfigByKey("dingTalkUploadToDingPanUnionid",['value'])['value'];
//        $unionId = TokenFuc::getCurrentStaffInfo()['dingTalkUnionId'];
        $fileInfo = $this->getFileId($fileId);
        $fileSize = Storage::response($fileInfo['file_url'], $fileInfo['file_name'])->headers->get('content-length');
        $filePath = PathFunc::getStorageApp().$fileInfo['file_url'];
        $fileName = $fileInfo['file_name'];
        $dingTalkUpload = new DingTalkUploadFacade();
        # 获取当前用户企业钉盘信息
        $spaceList = $dingTalkUpload->getSpacesListByUnionId($unionId)->spaces;
        $spaceId = 0;
        foreach($spaceList as $value){
            # 匹配钉盘条件为全员文件夹 空间状态为企业  空间大小无限制
//            if($value->spaceName == "全员文件夹" ){
//                $spaceId = $value->spaceId;
//                break;
//            }
            if($value->spaceName == AppFunc::getDingTalkConfig()::SAVE_FILE_NAME){
                $spaceId = $value->spaceId;
                break;
            }
        }
        if(!$spaceId){
            return ResponseFunc::returnJson(
                'error',
                null,
                '企业钉盘未找到',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }

        $mediaId = @$dingTalkUpload->uploadDingPanByOss(
            fileName: $fileName,
            filePath: $filePath,
            fileSize: $fileSize,
            parentId: 0,
            spaceId: $spaceId,
            unionId: $unionId
        );

        if(!$mediaId){
            return ResponseFunc::returnJson(
                'error',
                null,
                'STS鉴权失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }

        # 创建外层私有文件夹
        # 权限私有文件夹是为了防止内部人员直接通过钉盘访问文件
        $authControllerParentSpace = $dingTalkUpload->createSpaceBySpaceName("DMS",$unionId,$spaceId);
        $authControllerParentSpaceId = $authControllerParentSpace->fileId;

        # 创建父文件夹
        # 文件归类地址
        $parentSpaceName = $dingTalkUpload->createSpaceBySpaceName($parentSpaceName,$unionId,$spaceId,$authControllerParentSpaceId);
        $parentSpaceId = $parentSpaceName->fileId;

        # 创建一个新的文件夹
        # 文件实际上传位置
        $customSpaceInfo = $dingTalkUpload->createSpaceBySpaceName($createSpaceName,$unionId,$spaceId,$parentSpaceId);
        $customSpaceId = $customSpaceInfo->fileId;
        return $dingTalkUpload->addFileToDingTalkByFileName(
            fileName: $fileName,
            createUnionId: $unionId,
            spaceId: $spaceId,
            mediaId: $mediaId,
            parentId: $customSpaceId
        );

    }
}